package com.evil.account.strategy.login;

import com.evil.account.enums.login.AuthTypeEnum;
import com.evil.common.account.bo.AuthUserBO;
import com.evil.common.account.dto.LoginReqDTO;
import com.evil.common.account.dto.LogoutReqDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@Service
@AllArgsConstructor
public class AuthServiceStrategy {

    private final List<AuthService> authServiceList;

    /**
     * 登录
     *
     * @param loginReqDTO loginReqDTO
     * @return AuthUserBO
     */
    public AuthUserBO login(LoginReqDTO loginReqDTO) {
        return this.getAuthService(loginReqDTO.getLoginType()).login(loginReqDTO);
    }

    private AuthService getAuthService(Integer loginType) {
        AuthTypeEnum authTypeEnum = AuthTypeEnum.findByType(loginType);
        return authServiceList.stream().filter(e -> e.type() == authTypeEnum)
                .findFirst().orElseThrow(AuthTypeEnum::getBusinessException);
    }
}
